package handler

import (
	"context"
	"dh.com/store/api/goods/client"
	"dh.com/store/srv/goods/proto/goods"
	"encoding/json"
	api "github.com/micro/go-micro/api/proto"
	"github.com/micro/go-micro/errors"
)

type Goods struct{}

func NewGoods() *Goods {
	return &Goods{}
}

const ErrorId = "com.dh.store.api.goods"

func (e *Goods) Add(ctx context.Context, req *api.Request, rsp *api.Response) error {
	goodClient, _ := client.GoodsFromContext(ctx)

	var g goods.GoodsDTO
	err := json.Unmarshal([]byte(req.Body), &g)
	if err != nil {
		return errors.InternalServerError(ErrorId, err.Error())
	}

	_, err = goodClient.Add(ctx, &g)
	if err != nil {
		return errors.InternalServerError(ErrorId, err.Error())
	}

	return nil
}

func (e *Goods) Update(ctx context.Context, req *api.Request, rsp *api.Response) error {
	goodClient, _ := client.GoodsFromContext(ctx)

	var g goods.GoodsDTO
	err := json.Unmarshal([]byte(req.Body), &g)
	if err != nil {
		return errors.InternalServerError(ErrorId, err.Error())
	}

	_, err = goodClient.Update(ctx, &g)
	if err != nil {
		return errors.InternalServerError(ErrorId, err.Error())
	}

	return nil
}

func (e *Goods) Delete(ctx context.Context, req *api.Request, rsp *api.Response) error {
	goodClient, _ := client.GoodsFromContext(ctx)

	id, ok := req.Get["id"]
	if !ok || len(id.Values) <= 0 {
		return errors.InternalServerError(ErrorId, "参数缺失！")
	}

	_, err := goodClient.Delete(ctx, &goods.KeyRequest{Id: id.Values[0]})
	if err != nil {
		return errors.InternalServerError(ErrorId, err.Error())
	}

	return nil
}

func (e *Goods) FindOne(ctx context.Context, req *api.Request, rsp *api.Response) error {
	goodClient, _ := client.GoodsFromContext(ctx)

	id, ok := req.Get["id"]
	if !ok || len(id.Values) <= 0 {
		return errors.InternalServerError(ErrorId, "参数缺失！")
	}

	rst, err := goodClient.FindOne(ctx, &goods.KeyRequest{Id: id.Values[0]})
	if err != nil {
		return errors.InternalServerError(ErrorId, err.Error())
	}

	s, err := json.Marshal(rst.Goods)
	if err != nil {
		return errors.InternalServerError(ErrorId, err.Error())
	}
	rsp.Body = string(s)

	return nil
}

func (e *Goods) FindAll(ctx context.Context, req *api.Request, rsp *api.Response) error {
	goodClient, _ := client.GoodsFromContext(ctx)

	rst, err := goodClient.FindAll(ctx, &goods.NullRequest{})
	if err != nil {
		return errors.InternalServerError(ErrorId, err.Error())
	}

	s, err := json.Marshal(rst.Goods)
	if err != nil {
		return errors.InternalServerError(ErrorId, err.Error())
	}
	rsp.Body = string(s)

	return nil
}

//// Goods.Call is called by the API as /goods/call with post body {"name": "foo"}
//func (e *Goods) Call(ctx context.Context, req *api.Request, rsp *api.Response) error {
//	log.Log("Received Goods.Call request")
//
//	// extract the client from the context
//	goodsClient, ok := client.GoodsFromContext(ctx)
//	if !ok {
//		return errors.InternalServerError("com.dh.store.api.goods.goods.call", "goods client not found")
//	}
//
//	// make request
//	response, err := goodsClient.Call(ctx, &goods.Request{
//		Name: extractValue(req.Post["name"]),
//	})
//	if err != nil {
//		return errors.InternalServerError("com.dh.store.api.goods.goods.call", err.Error())
//	}
//
//	b, _ := json.Marshal(response)
//
//	rsp.StatusCode = 200
//	rsp.Body = string(b)
//
//	return nil
//}
